<template>
  <div class="app-container">
    <div class="app-container_conter">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px" style="display: flex;justify-content: space-between">
      <el-form-item label="企业名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item style="width: 150px;text-align: right">
        <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info"  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            style="background: #0793FF;color:#FFFFFF "
            size="mini"
            @click="handleAdd"
            v-hasPermi="['insurance:InsuredInformation:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="info"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['insurance:InsuredInformation:remove']"
        >删除
        </el-button>
      </el-col>

      <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="employerList" @selection-change="handleSelectionChange" :header-cell-style="tableHeaderColor">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="企业名称" align="center" prop="name" fixed min-width="170px"/>
      <el-table-column label="统一社会信息代码" align="center" prop="credit_no" min-width="180px"/>
      <el-table-column label="联系人" align="center" prop="contact" min-width="100px"/>
      <el-table-column label="联系方式" align="center" prop="phone" min-width="130px"/>
      <el-table-column label="营业执照" align="center" prop="business_img" min-width="100px">
        <template slot-scope="scope">
          <el-image
              style="width:50px; height: 50px;margin-right: 5px;margin-bottom: 5px"
              :src="scope.row.business_img"
              :preview-src-list="[scope.row.business_img]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="通讯地址" align="center" prop="mail_address" min-width="180px"/>
      <el-table-column label="邮编" align="center" prop="email_code" min-width="120px"/>
      <el-table-column label="邮箱" align="center" prop="email" min-width="130px"/>
      <el-table-column label="状态" align="center" prop="email" min-width="150px">
        <template slot-scope="scope">

          <span v-if="scope.row.status=='1' || scope.row.status=='0'">
               {{ scope.row.status == '1' ? '通过' : '未通过' }}
           </span>
          <span v-if="scope.row.status=='2'">
                审核中
         </span>
          <!--         </span>-->

        </template>

      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" min-width="180px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['insurance:InsuredInformation:edit']"
          >修改
          </el-button>

          <el-button
              style="color:#FF0000 "
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['insurance:InsuredInformation:remove']"
          >删除
          </el-button>
          <el-button
              v-if="scope.row.is_auth==1"
              size="mini"
              type="text"
              @click="getSwitch(scope.row)"
          >审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    </div>
    <pagination
        style="background: transparent"
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改用工单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">


        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="credit_no">
          <el-input v-model="form.credit_no" placeholder="请输入"/>
        </el-form-item>
        <el-row>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="所属地区" prop="contact">-->
          <!--              <el-cascader-->
          <!--                  size="large"-->
          <!--                  :options="options"-->
          <!--                  v-model="selectedOptons"-->
          <!--                  @change="addressChange"-->
          <!--              ></el-cascader>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="行业类别" prop="contact">
              <el-select v-model="form.industry" placeholder="请选择" filterable>
                <el-option
                    v-for="dict in industryOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictLabel">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="contact">
              <el-input v-model="form.contact" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone" label-width="100px">
              <el-input v-model="form.phone" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="通讯地址" prop="mail_address">
          <el-input v-model="form.mail_address" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="邮编" prop="email_code">
          <el-input v-model="form.email_code" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark ">
          <el-input v-model="form.remark " placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="营业范围" prop="business_scope">
          <el-input v-model="form.business_scope" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="营业执照" prop="business_img">
          <div style="display: flex">
            <div v-if="form.business_img!=null" style="display: flex;flex-flow: wrap;align-items: center;">
              <img :src="form.business_img" style="margin-right: 5px;margin-bottom: 5px" width="130" height="100px">
            </div>
            <el-upload
                ref="upload"
                action
                :before-upload="beforeUploadForm"
                :http-request="handleUpdateForm"
                :on-preview="handlePreview"
                :show-file-list="false"
            >
              <div
                  style="width: 100px;height: 100px;line-height: 100px;text-align: center;border: 1px solid #d9d9d9">
                <i class="el-icon-plus avatar-uploader-icon" style="font-size: 30px;color: #d9d9d9"></i>
              </div>
            </el-upload>
          </div>

        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--    审核状态-->
    <el-dialog :title="title1" :visible.sync="open1" width="400px" append-to-body>
      <el-form :model="ruleForm" :rules="rules1" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="状态" prop="status">
            <el-radio v-model="ruleForm.status" label="0">不通过</el-radio>
            <el-radio v-model="ruleForm.status" label="1">通过</el-radio>
        </el-form-item>

        <el-form-item>
          <el-button @click="open1=false">取消</el-button>
          <el-button type="primary" @click="submitStatus">确认</el-button>

        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEmployer,
  getEmployer,
  delEmployer,
  addEmployer,
  updateEmployer,
  exportEmployer
} from "@/api/insurance/employer";
import {regionData, CodeToText} from 'element-china-area-data'
import {
  get_policy_holder,
  check_employer,
  add_edit_company,
  get_edit_company,
  uploadFile
} from '@/api/policyHolder/ApplicantInformation'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {get_base_industry} from "@/api/insurance/repordRecord"
import {uploadTmage1} from "@/api/base/template";
import Template from "../../base/template";


export default {
  name: "index",
  components: {
    Template,
    Treeselect
  },
  data() {
    return {
      status: '0',//审核状态
      url: process.env.VUE_APP_BASE_API,//地址
      fileList: [],
      industry1: null,//行业类型
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用工单位表格数据
      employerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      title1: '',//审核
      open1: false,//打开
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        user_id: null,
        role_id: null,
        type: 2,//被投保单位
        name: null,
        is_need: 1
      },
      // 表单参数
      form: {
        type: "1"
      },
      //审核参数
      ruleForm: {
        employer_id: '',
        status:'0'
      },
      rules1:{
        status: [
          {required: true, message: "审核状态不为空", trigger: "blur"}
        ],
      },
      // 表单校验
      rules: {
        name: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ],
        credit_no: [
          {required: true, message: "统一社会信用代码不能为空", trigger: "blur"},
          {pattern: /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/, message: '请输入正确的社会信用代码'}
        ],
        contact: [
          {required: true, message: "联系人不能为空", trigger: "blur"}
        ],
        phone: [
          {required: true, message: "电话不能为空", trigger: "blur"},
          {pattern: /^(1[3|4|5|6|7|8|9])\d{9}$|^0\d{2,3}-?\d{7,8}$/, message: '请输入正确的联系人电话'}
        ],
        mail_address: [
          {required: true, message: "通讯地址不能为空", trigger: "blur"}
        ],
        email_code: [
          {required: true, message: "邮编不能为空", trigger: "blur"},
          {pattern: /[1-9]\d{5}(?!\d)/, message: '请输入正确的邮编'}
        ],
        email: [
          {required: true, message: '邮箱号码', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱号码', trigger: ['blur', 'change']}
        ],
        business_scope: [
          {required: true, message: '请输入营业范围', trigger: 'blur'},
        ],
        business_img: [
          {required: true, message: '请上传营业执照', trigger: 'blur'},
        ],
      },
      industryOptions: [],//行业类别
      selectedOptons: [],
      options: regionData,
      disabled: false,

    };
  },

  created() {
    // console.log('status', this.status)
    this.queryParams.user_id = JSON.parse(localStorage.getItem('userInfo')).userId;
    var info = JSON.parse(localStorage.getItem('userInfo'));
    if (info.roles.length != 0) {
      this.queryParams.role_id = info.roles[0].roleId
    }
    this.getList();

    this.getDicts("Industry_category").then(res => {
      this.industryOptions = res.data
      // console.log('res', res)
    })
  },
  methods: {
    /* 修改表头颜色 */
    tableHeaderColor({ rowIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #304156;color: #fff;font-weight: 400;font-family: Microsoft YaHei;'
      }
    },
    //获取所属地区
    addressChange(arr) {
      this.selectedOptons = CodeToText[arr[0]] + '/' + CodeToText[arr[1]] + '/' + CodeToText[arr[2]]
      // console.log('arr', arr)
      // console.log('arr', CodeToText[arr[0]], CodeToText[arr[1]], CodeToText[arr[2]]);
      // console.log('selectedOptons', this.selectedOptons)
    },
    //状态审核
    getSwitch(row) {
      // console.log('2222222222', row)
      this.ruleForm.employer_id = row.id
      // this.ruleForm.status = row.status
      this.open1 = true;
      this.title1 = '审核'

    },
    //点击确认按钮提交审核
    submitStatus(){
      // console.log('ruleForm',this.ruleForm)
      check_employer(this.ruleForm).then(res => {
                if (res.code == 1) {
                  this.$message({
                    message: res.msg,
                    type: 'success'
                  });
                  this.open1 = false;
                  this.getList();
                }
      })
    },
    /** 查询用工单位列表 */
    getList() {
      this.loading = true;
      // console.log('queryParams',this.queryParams)
      get_policy_holder(this.queryParams).then(response => {
        this.loading = false;
        // console.log('查询所有数据', response)
        if (response.code == 1) {
          this.employerList = response.data.list;
          // this.employerList.forEach(v => {
          //   v.status = v.status.toString()
          // })
          this.total = response.data.count;
        }
      });
    },


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        user_id: this.queryParams.user_id,// 的当前登录人id
        id: null,//数据id，添加时传空，编辑时传具体id
        type: 2,//1投保单位  2被投保单位
        name: null,//公司名称
        credit_no: null,// 统一社会信用代码
        contact: null,//联系人
        phone: null,//联系人电话
        mail_address: null,//通讯地址
        email_code: null,//邮编
        email: null,//邮箱号码
        industry: null,// 行业类别
        business_scope: null,// 营业范围
        business_img: null,// 营业执照
        remark: null,// 备注
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.industry1 = null;
      this.reset();
      this.open = true;
      this.title = "添加被保险人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.fileList = [];
      this.reset();
      const id = row.id || this.ids
      var obj = {
        id: id,
        type: 1
      }
      get_edit_company(obj).then(response => {
        // console.log('详情', response)
        this.form = response.data;
        this.industry1 = response.data.industry
        this.open = true;
        this.title = "被保险人编辑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // console.log('for', this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            add_edit_company(this.form).then(response => {
              // console.log('response编辑',response)
              if (response.code == 1) {
                this.msgSuccess(response.msg);
              }
              this.open = false;
              this.getList();
            });
          } else {
            add_edit_company(this.form).then(response => {
              // console.log('response添加',response)
              if (response.code == 1) {
                this.msgSuccess(response.msg);
              }
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      var obj = {
        id: ids,
        type: 2,//1获取  2删除
      }
      var that = this;
      this.$confirm('是否确认删除用工单位编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        get_edit_company(obj).then(res => {
          if (res.code == 1) {
            that.msgSuccess(res.msg);
            that.getList();
          }
          // console.log('删除', res)
        })
      }).then(() => {

      })
    },
    // 上传身份证式显示
    beforeUploadForm(file) {
      // console.log('file12', file)
      const isPng = file.type === 'image/png' || 'image/jpg' || 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isPng) {
        this.$message.error('上传营业执照只能是png/jpg/jpeg格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传营业执照大小不能超过2MB!');
      }
      return isPng && isLt2M;
    },
    //上传身份证
    handleUpdateForm(param) {
      // console.log('param', param)
      this.fileList.push(param.file);

      let that = this;
      let formData = new FormData();
      this.fileList.forEach(v => {
        formData.append('file', v);
      })
      uploadFile(formData).then(response => {
        // console.log('res图片', response)

        // this.urlImage = response.url;
        this.form.business_img = response.data
        // // this.formData.seal = response.fileName;
        // if (this.form.businesslicense !='') {
        //   this.show = false;
        // } else {
        //   this.show = true;
        // }


      })

      // console.log('fileListdf', this.fileList)
    },
    handleRemove(file, fileList) {
      // console.log(file, fileList);
    },
    handlePreview(file) {
      // console.log(file);
    }
  }
};
</script>
<style lang="scss">
.app-container_conter{
  background: #F8FBFF;
  padding:30px;
  border-radius: 10px;
  box-sizing: border-box;
}
.tablescope {

  .el-switch__label--left {
    position: relative;
    left: 70px;
    color: #fff;
    z-index: -1111;
    font-size: 10px !important;
  }

  .el-switch__core {
    width: 60px !important;
  }

  .el-switch__label--right {
    position: relative;
    right: 58px;
    color: #777777;
    z-index: -1111;
  }

  .el-switch__label--right.is-active {
    z-index: 1111;
    color: #fff !important;
  }

  .el-switch__label--left.is-active {
    z-index: 1111;
    //color: #9c9c9c !important;
    color: #FFFFFF;
  }


}

.el-switch__label {
  font-size: 6px;
}

</style>

